package com.sdleyou.treepro.common.model.convert;

import com.sdleyou.treepro.common.model.dao.UserDO;
import com.sdleyou.treepro.common.model.vo.UserRankVO;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class UserDO2UserRankVO {
    public static UserRankVO convert(UserDO userDO, String userId) {
        UserRankVO userRankVO = new UserRankVO();
        userRankVO.setUserId(userDO.getUserId());
        userRankVO.setUserName(userDO.getNickname());
        userRankVO.setFace(userDO.getFace());
        Integer value = userDO.getTotalGrow() == null ? 0 : userDO.getTotalGrow();
        userRankVO.setTreeSum(formatValue(value));
        if (userDO.getUserId().equals(userId)) {
            userRankVO.setIsMe(true);
        } else {
            userRankVO.setIsMe(false);
        }
        return userRankVO;
    }

    public static String formatValue(Integer value) {
        String formatValue = "";
        if (value < 1000) {
            formatValue = value.toString();
        } else if (value >= 1000 && value < 10000) {
            formatValue = formatDecimal(value, 1000) + "K";
        } else if (value >= 10000 && value < 100000) {
            formatValue = formatDecimal(value, 10000) + "M";
        } else if (value >= 100000 && value < 1000000) {
            formatValue = formatDecimal(value, 100000) + "G";
        } else {
            formatValue = formatDecimal(value, 1000000) + "T";
        }
        return formatValue;
    }

    public static String formatDecimal(Integer value, Integer molecule) {
        BigDecimal bv = new BigDecimal(value);
        BigDecimal bm = new BigDecimal(molecule);
        BigDecimal res = bv.divide(bm);
        return res.setScale(2, RoundingMode.HALF_UP).toString();
    }
}
